package com.ang.mybatisplus.base.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.util.Objects;

/**
 * @author Administrator
 */
@Slf4j
@Configuration
public class MybatisPlusDataSourceConfig {

    @Autowired
    private MyMetaObjectHandler metaObjectHandler;

    public static final String testSqlSessionFactory = "testSqlSessionFactory";
    public static final String testDruidDataSource = "testDruidDataSource";

    public MybatisPlusDataSourceConfig(){
        System.out.println("-------------------------------");
        System.out.println("MybatisPlusDataSourceConfig");
        System.out.println("-------------------------------");
    }

    @Bean(name = testDruidDataSource)
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
    public DruidDataSource druidDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(testSqlSessionFactory)
    public SqlSessionFactory baseSqlSessionFactory() {
        MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();

        try {
            //数据源
            sessionFactoryBean.setDataSource(druidDataSource());

            //自定义mapper的xml文件地址，当通用mapper提供的默认功能无法满足我们的需求时，可以自己添加实现，与mybatis写mapper一样
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            sessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/**/*.xml"));

            //自动填充
            GlobalConfig globalConfig = GlobalConfigUtils.defaults();
            globalConfig.setMetaObjectHandler(metaObjectHandler);
            sessionFactoryBean.setGlobalConfig(globalConfig);

            //定义分页插件，可以不配置，自动注入
//            MybatisConfiguration configuration = new MybatisConfiguration();
//            configuration.addInterceptor(testPaginationInterceptor);
//            sessionFactoryBean.setConfiguration(configuration);

            return sessionFactoryBean.getObject();
        } catch (Exception e) {
            log.error("配置Business的SqlSessionFactory失败，error:{}", e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }

}
